﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for Common
/// </summary>
/// 

namespace LinkCard.Utils
{
    public class hash1way
    {

        public static string SQLPatch(string sText)
        {
            return sText.Replace("'", "''").Trim();

        }

        public static string replaceContent(string sContent)
        {
            //string sSQL = "Select Ticker, ID from stox_tb_company order by Ticker";
            //DataTable dt = DataHelper.SelectTable(sSQL, false);
            //if (dt.Rows.Count > 0)
            //{
            //    for (int i = 0; i < dt.Rows.Count; i++)
            //    {
            //        string sTicker = dt.Rows[i]["Ticker"].ToString().ToUpper();  // ucase(trim(adoRecTickerInNewsContent("Ticker")))
            //        string sCompanyID = dt.Rows[i]["ID"].ToString();
            //        sContent = sContent.Replace(" " + sTicker + " ", " <a href='http://stoxplus.com/stoxpage/stoxpage.asp?MenuID=4&subMenuID=13&action=home&CompanyID=" + sCompanyID + "' style ='text-decoration:underline;'>" + sTicker + "</a> ");
            //        sContent = sContent.Replace(" " + sTicker + ",", " <a href='http://stoxplus.com/stoxpage/stoxpage.asp?MenuID=4&subMenuID=13&action=home&CompanyID=" + sCompanyID + "' style ='text-decoration:underline;'>" + sTicker + "</a>,");
            //        sContent = sContent.Replace(" " + sTicker + ".", " <a href='http://stoxplus.com/stoxpage/stoxpage.asp?MenuID=4&subMenuID=13&action=home&CompanyID=" + sCompanyID + "' style ='text-decoration:underline;'>" + sTicker + "</a>.");
            //        sContent = sContent.Replace("(" + sTicker + ")", "(<a href='http://stoxplus.com/stoxpage/stoxpage.asp?MenuID=4&subMenuID=13&action=home&CompanyID=" + sCompanyID + "' style ='text-decoration:underline;'>" + sTicker + "</a>)");
            //        sContent = sContent.Replace("<STRONG>" + sTicker + "</STRONG>", "<a href='http://stoxplus.com/stoxpage/stoxpage.asp?MenuID=4&subMenuID=13&action=home&CompanyID=" + sCompanyID + "' style ='text-decoration:underline;font-weight:bold;'>" + sTicker + "</a> ");
            //    }
            //}
            return sContent;
        }

        public static string GetPermission(string sUser)
        {
            string sPermission = "ERROR";
            //string sSQL = "SELECT Permission FROM [User] WHERE Username='" + sUser + "'";
            //DataTable dt = DataHelper.SelectTable(sSQL, false);
            //if (dt.Rows.Count > 0) sPermission = dt.Rows[0]["Permission"].ToString();
            return sPermission;
        }

        public static bool CheckPermission(string sUserPermission, string sRole)
        {

            bool result = false;
            int nPossion = 0;
            //int nCheck = 0;

            switch (sRole)
            {
                case "SYSADMIN":
                    nPossion = 1;
                    break;
                case "SUPPLIER":
                    nPossion = 2;
                    break;
                case "DEAL":
                    nPossion = 3;
                    
                    break;
                case "CUSTOMER":
                    nPossion = 4;
                    
                    break;
                case "PAYMENT":
                    nPossion = 5;
                    break;
                
                default:
                    nPossion = 1;
                    break;
            }


            //if (nPossion != 3)
            //{
            //    if (sUserPermission.Substring(nPossion - 1, 1) == "1")
            //    {
            //        result = true;
            //    }
            //    else
            //    {
            //        result = false;
            //    }
            //}
            //else
            //{
            //    if (sUserPermission.Substring(2, 1) == nCheck.ToString())
            //    {
            //        result = true;
            //    }
            //    else
            //    {
            //        result = false;
            //    }
            //}

            if (sUserPermission.Substring(nPossion - 1, 1) == "1")
            {
                result = true;
            }
            else
            {
                result = false;
            }



            return result;
        }

        public static string ShowPageNavigation(int lCurrentPage, int lPageCount)
        {

            string temp = "";
            string temp0 = "";
            string temp1 = "";
            string temp2 = "";
            string temp3 = "";
            string temp4 = "";

            int canTren, canDuoi;
            canTren = lCurrentPage + 2; // Biến cận trên
            if (lPageCount - lCurrentPage == 0)
            {
                canTren = lPageCount;
                canDuoi = canTren - 2;
            }
            else if (lPageCount - lCurrentPage == 1)
            {
                canTren = lPageCount;
                canDuoi = canTren - 3;
            }

            else
            {
                canDuoi = canTren - 4;
            }
            if (canDuoi <= 0) canDuoi = 1;
            if (canDuoi > 1) temp1 = "<a href=\"javascript:setValue('1');\"> Trang đầu </a>&nbsp;";
            if (lCurrentPage > 3) temp2 += "<a href=\"javascript:setValue('" + (lCurrentPage - 3).ToString() + "');\"> ... </a>&nbsp;";

            for (int i = canDuoi; i <= canTren; i++)
            {
                if (i == lCurrentPage)
                {
                    temp0 += "<a class=\"current\"> " + i + " </a>&nbsp;";
                }
                else
                {
                    temp0 += "<a href=\"javascript:setValue('" + i + "');\"> " + i + " </a>&nbsp;";
                }


                if (lCurrentPage <= lPageCount - 3) temp3 = "<a href=\"javascript:setValue('" + (lCurrentPage + 3).ToString() + "');\"> ... </a>&nbsp;";
                if (canTren < lPageCount) temp4 = "<a href=\"javascript:setValue('" + lPageCount + "');\"> Trang cuối </a>&nbsp;";
            }

            return temp1 + temp2 + temp0 + temp3 + temp4;
        }
    }

}